$(function(){


	// 听力原文与题目的切换效果
	$(".text-article").click(function(){
		$(this).addClass("textbc").siblings().removeClass("textbc");
		$("#text_wrap").removeClass("hidden");
		$("#listen_wrap").addClass("hidden");
	});
	$(".text-info").click(function(){
		$(this).addClass("textbc").siblings().removeClass("textbc");
		$("#text_wrap").addClass("hidden");
		$("#listen_wrap").removeClass("hidden");
	});

	// 作文输出
	var $textarea=$("[class=record-text] textarea");
	var text=$textarea.val();
	$textarea.html(text);





    var showtext=[];
    var sourse=[];
    var $model=$("#voicemodel").clone(true).removeClass("hidden").addClass("users-voice").removeAttr("id");
    que_info.num=2;
    $.ajax({
        url:"?r=ajax/comment",
        data:que_info,
        dataType:'json',
        method:'get',
        success:function(data){
            var length=data.length;
            for(var i=0; i<length; i++){
                var $clone=$model.clone(true);
                sourse.push(data[i].comment_content);
                showtext.push(changeText(changeData(data[i].comment_content)));
                $clone=setHot(data[i],$clone);
                $clone.insertBefore(".add-more");
                $clone.hide();
                if (i<2) {
                    $clone.slideDown(500);
                }
            }

            //展开全文
            
            var $viewall=$(".view-all");
            var $comment=$(".usercomment");
            var flag=true;
            $comment.addClass("notshow");
            var $chart=$(".allchart");
            $chart.each(function(){
                $(this).hide();
            });
            $viewall.click(function(){
                var index=$viewall.index(this);
                var $thiscomment=$(this).siblings(".user-comments").find(".usercomment");
                if ($thiscomment.hasClass("notshow")) {
                    $thiscomment.html(sourse[index]).removeClass("notshow");
                    $chart.eq(index).show();
                    $(this).text("点击收起全文");
                }else{
                    $thiscomment.html(showtext[index]).addClass("notshow");
                    $chart.eq(index).hide();
                    $(this).text("点击展开全文");
                }
            });

            var $addmore=$(".add-more");
            $addmore.click(function(){
                var n=0;
                $(".users-voice").each(function(index,item){
                    var $this=$(this);
                    if (!$this.is(":visible")&&n<5) {
                        $this.slideDown(500);
                        n++;
                    }
                })
            });

                    //点赞功能,只能点赞一次,点赞后添加一个hasClick的类

            $(".good-times>span").click(function(){
                var $this=$(this);

                //获取当前所在的下标index
                var $thisvoice=$this.closest(".good-times");
                var index=$(".good-times").index($thisvoice);
                var tarid=data[index].user_id;

                var $thisgood=$this.find(".goods");
                if (!$this.hasClass("isgood")) {
                    $thisgood.text(parseInt($thisgood.text())+1+'');
                    $this.addClass("isgood");
                    $.ajax({
                        url:'?r=ajax/like',
                        method:'post',
                        dataType:'json',
                        data:{
                            'tar':tarid,
                            'sou':current_userid,
                            'class2':que_info.class2,
                            'class1':que_info.class1,
                            'question':que_info.question,
                            'status':'like'
                        }
                    })
                }else{
                    $thisgood.text(parseInt($thisgood.text())-1+'');
                    $this.removeClass("isgood");
                    $.ajax({
                        url:'?r=ajax/like',
                        method:'post',
                        dataType:'json',
                        data:{
                            'tar':tarid,
                            'sou':current_userid,
                            'class2':que_info.class2,
                            'class1':que_info.class1,
                            'question':que_info.question,
                            'status':'unlike'
                        }
                    })
                }
            });

            $(".users-voice").mouseenter(function(){
                var $this=$(this);
                //获取当前所在的下标index
                var index=$(".users-voice").index(this);
                var tarid=data[index].user_id;
                $.ajax({
                    url:'?r=ajax/view',
                        method:'post',
                        dataType:'json',
                        data:{
                            'tar':tarid,
                            'sou':current_userid,
                            'class2':que_info.class2,
                            'class1':que_info.class1,
                            'question':que_info.question,
                        }
                })
            })
        }
    });

    var $share=$(".share");
    $share.click(function(){
        var comments=$("#user-composition").html();
        var que_info_comment=que_info;
        que_info_comment.comment=comments;
        $succeed.show();
        $.ajax({
            method:'post',
            url:'?r=ajax/comment',
            dataType:'json',
            data:que_info_comment,
            success:function(data){
                if (data.state) {
                    $(".users-voice").remove();
                    var showtext=[];
                    var sourse=[];
                    que_info.num=2;
                    $.ajax({
                        url:"?r=ajax/comment",
                        data:que_info,
                        dataType:'json',
                        method:'get',
                        success:function(data){
                            var length=data.length;
                            for(var i=0; i<length; i++){
                                var $clone=$model.clone(true);
                                sourse.push(data[i].comment_content);
                                showtext.push(changeText(changeData(data[i].comment_content)));
                                $clone=setHot(data[i],$clone);
                                $clone.insertBefore(".add-more");
                                $clone.hide();
                                if (i<2) {
                                    $clone.slideDown(500);
                                }
                            }
                            //展开全文
                            var $viewall=$(".view-all");
                            var $comment=$(".usercomment");
                            var flag=true;
                            $comment.addClass("notshow");
                            var $chart=$(".allchart");
                            $chart.each(function(){
                                $(this).hide();
                            });
                            $viewall.click(function(){
                                var index=$viewall.index(this);
                                var $thiscomment=$(this).siblings(".user-comments").find(".usercomment");
                                if ($thiscomment.hasClass("notshow")) {
                                    $thiscomment.html(sourse[index]).removeClass("notshow");
                                    $chart.eq(index).show();
                                    $(this).text("点击收起全文");
                                }else{
                                    $thiscomment.html(showtext[index]).addClass("notshow");
                                    $chart.eq(index).hide();
                                    $(this).text("点击展开全文");
                                }
                            });

                            var $addmore=$(".add-more");
                            $addmore.click(function(){
                                var n=0;
                                $(".users-voice").each(function(index,item){
                                    var $this=$(this);
                                    if (!$this.is(":visible")&&n<5) {
                                        $this.slideDown(500);
                                        n++;
                                    }
                                })
                            });

                            //点赞功能,只能点赞一次,点赞后添加一个hasClick的类
                            $(".good-times>span").click(function(){
                                var $this=$(this);
                                //获取当前所在的下标index
                                var $thisvoice=$this.closest(".good-times");
                                var index=$(".good-times").index($thisvoice);
                                var tarid=data[index].user_id;

                                var $thisgood=$this.find(".goods");
                                if (!$this.hasClass("isgood")) {
                                    $thisgood.text(parseInt($thisgood.text())+1+'');
                                    $this.addClass("isgood");
                                    $.ajax({
                                        url:'?r=ajax/like',
                                        method:'post',
                                        dataType:'json',
                                        data:{
                                            'tar':tarid,
                                            'sou':current_userid,
                                            'class2':que_info.class2,
                                            'class1':que_info.class1,
                                            'question':que_info.question,
                                            'status':'like'
                                        }
                                    })
                                }else{
                                    $thisgood.text(parseInt($thisgood.text())-1+'');
                                    $this.removeClass("isgood");
                                    $.ajax({
                                        url:'?r=ajax/like',
                                        method:'post',
                                        dataType:'json',
                                        data:{
                                            'tar':tarid,
                                            'sou':current_userid,
                                            'class2':que_info.class2,
                                            'class1':que_info.class1,
                                            'question':que_info.question,
                                            'status':'unlike'
                                        }
                                    })
                                }
                            });
                            $(".users-voice").mouseenter(function(){
                                var $this=$(this);
                                //获取当前所在的下标index
                                var index=$(".users-voice").index(this);
                                var tarid=data[index].user_id;
                                $.ajax({
                                    url:'?r=ajax/view',
                                        method:'post',
                                        dataType:'json',
                                        data:{
                                            'tar':tarid,
                                            'sou':current_userid,
                                            'class2':que_info.class2,
                                            'class1':que_info.class1,
                                            'question':que_info.question
                                        }
                                })
                            })
                        }
                    })
                }
            }
        })
    })
;

    //分享中的动画效果
    var $succeed=$("#comment-succeed");
    $succeed.hide();
    $(document).ajaxStop(function(){
        $succeed.hide();
    })
});
function changeText(val){
    if (val.length>120) {
        return val.slice(0,120)+'...';
    }else{
        return val;
    }
}
function changeData(val){
    val=val.replace(/\s{2,}|\<br\s*\/\>/g,' ')     //超过两个空字符或者是<br></br>都替换成空格
    return val;
}
function setHot(data,$demo){
    $demo.find(".play-record audio").attr("src",data.url);
    if (data.photo) {
        $demo.find(".user-pic img").attr("src",data.photo);
    }else{
        $demo.find(".user-pic img").attr("src","<?=Url::to('@web/images/default.jpg');?>");
    }
    $demo.find(".user-id").text(data.name);
    $demo.find(".user-date").text(data.comment_time);
    $demo.find(".views").text(data.play_num);
    $demo.find(".goods").text(data.like_num);
    $demo.find(".usercomment").html(changeText(changeData(data.comment_content)));
    var tx=$demo.find(".usercomment").text();
    var $cmts=$demo.find(".user-comments");
    var removetx='';
    if ($cmts.find(".selfText")) {
        removetx=$demo.find(".user-comments").find(".selfText").text();
    }
    tx=tx.replace(removetx,'');
    $demo.find(".usercomment").text(changeText(tx));

    $demo.find(".chartone>span").text(isNull(data.readtheme));
    $demo.find(".chartone ul>li:nth-child(1) span").text(isNull(data.readhand1))
    $demo.find(".chartone ul>li:nth-child(2) span").text(isNull(data.readhand2))
    $demo.find(".chartone ul>li:nth-child(3) span").text(isNull(data.readhand3))
    if (data.listentheme=='null') {
        $demo.find(".charttwo").remove();
    }else{
        $demo.find(".charttwo>span").text(isNull(data.listentheme));
        $demo.find(".charttwo ul>li:nth-child(1) span").text(isNull(data.listenhand1))
        $demo.find(".charttwo ul>li:nth-child(2) span").text(isNull(data.listenhand2))
        $demo.find(".charttwo ul>li:nth-child(3) span").text(isNull(data.listenhand3))
    }
    var likepeo=data.like_ids;
    if (likepeo.indexOf(current_userid)!=-1&&likepeo.length>0) {
        $demo.find(".good-times>span").addClass("isgood");
    }
    return $demo;
}
function isNull(val){
    if (val=='null') {
        return ' ';
    }else{
        return val;
    }
}
//秒转化为分钟格式函数
function secToMinx(xsec){
	xsec=parseInt(xsec,10);
	var getstr=function(val){
		if (val<10) {return '0'+val}
		else return ''+val;
	};
	var min=parseInt(xsec/60);
	var sec=parseInt(xsec-min*60);
	return getstr(min)+":"+getstr(sec);
}
$(window).load(function(){
    //作文字数统计
    var $write=$("#user-composition");
    var $words=$("#countnum");
    $words.text('0');
    var write=$write.text();
    var parts=write.split(/[^a-zA-Z\d]+/);
    $words.text(parts.length-2);
    $write.keydown(function(){
        var $this=$(this);
        write=$this.text();
        parts=write.split(/[^a-zA-Z\d]+/);
        $words.text(parts.length-2);
    });
    if (document.getElementsByTagName("audio").length>0) {
            var $pauseplay=$(".pause-play");
            var audio=document.getElementsByTagName("audio")[0];
            // console.log(audio.duration);
            var endsec=audio.duration;
            var isPlay=false;
            var $starttime=$(".start-time");
            var $endtime=$(".end-time");
            var $totalline=$(".play-line");
            var $curline=$(".play-current-line");
            var $playpoint=$(".play-point");
            var $faplay=$pauseplay.find('i');
            var $voltotalline=$(".volume-line");
            var volleft=$voltotalline.offset().left;
            var volwidth=$voltotalline.width();
            var $volpoint=$voltotalline.children('[class=volume-point]');
            var $volcurline=$voltotalline.children('[class=volume-current-line]');
            var play=null;
            $endtime.text(secToMinx(endsec));
            $pauseplay.click(function(){
                isPlay=!isPlay;
                if (isPlay) {
                    $faplay.removeClass('fa-play').addClass('fa-pause');
                    audio.play();
                    play=setInterval(function(){
                        var curtime=audio.currentTime;
                        $starttime.text(secToMinx(curtime));
                        var percentage=curtime/endsec;
                        var pointleft=percentage*$totalline.width();
                        var curlinewidth=pointleft;
                        if (!audio.ended) {
                            $playpoint.css("left",pointleft-2+'px');
                            $curline.css("width",curlinewidth+'px');
                            // console.log("play");
                        }else{
                            clearInterval(play);
                            audio.current=0;
                            isPlay=false;
                            $faplay.removeClass('fa-pause').addClass('fa-play');
                        }
                    },1000)
                }
                else{
                    $faplay.removeClass('fa-pause').addClass('fa-play');
                    audio.pause();
                    clearInterval(play);
                }
            });
            $totalline.click(function(e){
                var totalwith=$totalline.width();
                var left=e.pageX-$totalline.offset().left;
                left=left<0?0:left;
                left=left>totalwith?totalwith:left;
                audio.currentTime=(left/totalwith)*endsec;
                isPlay=false;
                $pauseplay.click();
                // console.log("play");
            });
            var isDown=false;
            $playpoint.mousedown(function(e){
                isDown=true;
            });
            $(window).mousemove(function(e){
                if (isDown) {
                    var totalwith=$totalline.width();
                    var left=e.pageX-$totalline.offset().left;
                    left=left<0?0:left;
                    left=left>totalwith?totalwith:left;
                    $playpoint.css("left",left-2+'px');
                    $curline.css("width",left+'px');
                    audio.currentTime=(left/totalwith)*endsec;
                }
                if (isVolDown) {
                    setVol(e.pageX);
                }
            });
            $(window).mouseup(function(e){
                if (isDown) {
                    isDown=false;
                    isPlay=false;
                    $pauseplay.click();
                }
                if (isVolDown) {
                    isVolDown=false;
                    setVol(e.pageX);
                }
            });
            var isVolDown=false;
            $voltotalline.click(function(e){
                setVol(e.pageX);
            });
            $volpoint.mousedown(function(){
                isVolDown=true;
            });
            $(".volume-icon img").click(function(e){
                setVol(e.pageX);
            })
        }
    function setVol(event){
        var voltotal=$voltotalline.width();
        event=event<volleft?volleft:event;
        event=event>(volleft+volwidth)?(volleft+volwidth):event;
        var volume=(event-volleft)/volwidth;
        $volpoint.css("left",volume*volwidth-5  +'px');
        $volcurline.width(event-volleft);
        volume=volume.toFixed(2);
        volume=parseFloat(volume);
        audio.volume=volume;
    }
}) ;